// pages/addressAdd/addressAdd.js
var getLoc = require('../../utils/getLoc.js')
var request = require('../../utils/https.js');
Page({
  data: {
    option:{},
    id:'',
    name: '',
    phoneNum: '',
    address:'',
    detailAddress: '',
    uri: 'address/add.html',
  },
  //选择地址
  chAddress(){
    var that=this
    wx.chooseLocation({
      success: function (res) { 
        getLoc.getPCA(res, (addressBean)=>{
          that.setData({
            address:
            addressBean.REGION_PROVINCE + " "
            + addressBean.REGION_CITY + ""
            + addressBean.REGION_COUNTRY
          });
        });
      },
      fail: function (res) { 
        wx.showLoading({
          title: '选择地址失败',
          mask: true
        })
        setTimeout(function () {
          wx.hideLoading()
          that.chAddress();
        }, 2000)
      },
      complete: function (res) {
        console.log(res.name);
        that.setData({
          detailAddress: res.name
        });
       },
    })
  },
  //收货人赋值
  bindNameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  //手机号赋值
  bindPhoneInput: function (e) {
    var phone = e.detail.value;
    if (!(/^1[34578]\d{9}$/.test(phone))) {
      wx.showToast({
        title: '请填写正确联系电话',
        icon: 'none',
        mask: true
      })
    }
    if (phone.length >= 11) {
      wx.showToast({
        title: '请填写正确联系电话',
        icon: 'none',
        mask: true
      })
    }
    else{
      this.setData({
        phoneNum: e.detail.value
      })
    }
    
  },
  //详细地址赋值
  bindAddressInput: function (e) {
    this.setData({
      detailAddress: e.detail.value
    })
  },
  //保存
  addAddress: function () {
    var that = this;
    if (that.data.name.length == 0) {
      wx.showToast({
        title: '收货人不能为空',
        icon: 'none',
        mask: true
      })
    } else if (that.data.phoneNum.length == 0) {
      wx.showToast({
        title: '请填写正确联系电话',
        icon: 'none',
        mask: true
      })
    } else if (that.data.address.length== 0) {
      wx.showToast({
        title: '请选择所在省份',
        icon: 'none',
        mask: true
      })
    } else if (that.data.detailAddress.length == 0) {
      wx.showToast({
        title: '详细地址不能为空',
        icon: 'none',
        mask: true
      })
    } else {
      request.req(that.data.uri, {
        user_id: wx.getStorageSync('userId'),
        id: that.data.id,
        real_name: that.data.name,
        mobile: that.data.phoneNum,
        address: that.data.address,
        address_item: that.data.detailAddress,
      }, (err, res) => {
        var result = res.data;
        if (result == 1) { //地址保存成功
          wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
            success: function (res) {
              // success
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'loading',
            duration: 1500
          })
        }
      })
    }
  },
  useWx(){
    var that = this;
    wx.chooseAddress({
      success: function (res) {
        var address = res.provinceName + " " + res.cityName + "" + res.countyName;
        console.log(res)
        that.setData({
          name: res.userName,
          phoneNum: res.telNumber,
          address: address,
          detailAddress: res.detailInfo
        });
      }
    })
  },
  onLoad: function (options) {
    var that = this;
   if(options.way!='add'){
     wx.setNavigationBarTitle({
       title: '编辑地址',
     })
     that.setData({
       id:options.id,
       name: options.name,
       phoneNum: options.tel,
       address: options.address,
       detailAddress: options.detail,
       uri:'address/update.html'
     });
   }
  },
})